package untils

import (
	"fmt"
	"smart-health-platforms/config"
)

// 秒杀同步商品库存
func SyncSeckillStock(seckillId, stockCount int) {
	key := fmt.Sprintf("seckillGood_stock_%d", seckillId)
	for i := 0; i < stockCount; i++ {
		config.Rdb.LPush(config.Ctx, key, 1)
	}
}

// 扣减秒杀库存
func UpdateSeckillStock(seckillId int) bool {
	key := fmt.Sprintf("seckillGood_stock_%d", seckillId)
	_, err := config.Rdb.LPop(config.Ctx, key).Result()
	if err != nil {
		return false
	}
	return true
}

// 获取秒杀库存长度
func GetSeckillStock(seckillId int) int {
	key := fmt.Sprintf("seckillGood_stock_%d", seckillId)
	result, err := config.Rdb.LLen(config.Ctx, key).Result()
	if err != nil {
		return 0
	}
	return int(result)
}

// 返还库存
func ReturnSeckillStock(seckillId int) {
	key := fmt.Sprintf("seckillGood_stock_%d", seckillId)
	config.Rdb.LPush(config.Ctx, key, 1)
}
